<?php

namespace App\Jobs;

use Exception;
use App\Http\Controllers\MdClientController;
use App\Jobs\Middleware\RateLimited;
use App\Models\MdClient;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Support\Facades\Log;

class Address2CoordinateTransJob implements ShouldQueue
{
    use Queueable;

    /**
     * 任务可尝试的次数
     *
     * @var int
     */
    public $tries = 1000;

    /**
     * ins
     * @var MdClient|null
     * create_at: 2025-01-11 09:50:04
     * update_at: 2025-01-11 09:50:04
     */
    public $mdClientIns = null;

    /**
     * Create a new job instance.
     */
    public function __construct(public MdClient $mdClient)
    {
        $this->onQueue(MdClientController::ON_QUEUE);
        $this->mdClientIns = $mdClient;
    }

    /**
     * 中间件
     * @return array
     * create_at: 2025-01-11 09:49:56
     * update_at: 2025-01-11 09:49:56
     */
    public function middleware(): array
    {
        return [new RateLimited, (new WithoutOverlapping($this->mdClientIns->client_id))->releaseAfter(1)];
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        try {
            app(MdClientController::class)->handleJob($this->mdClientIns);
            $this->delete();
        } catch (Exception $e) {
            Log::error("Client ID 【{$this->mdClientIns->client_id}】： " + $e->getMessage());
        }
    }
}
